﻿function setWaring(tips) {
    return warning = '<div class="alert alert-error" style="display:block;"><strong>提示!</strong> ' + tips + '</div>';
}
var auth = (function () {

    // 当前路径 uri
    var url = url || Com.url.parse(window.location.href)['path'];

    // 鉴权信息
    //var authInfo = null;

    // 鉴权api
    //var authUrl = 'http://epg.lego.iqiyi.com/api/permission';

    // 登陆url
    //var loginUrl = 'http://lego.iqiyi.com/login?url=http://lego.iqiyi.com%2F';

    // 403 page
    //var url403 = 'http://lego.iqiyi.com/common/403.jsp';

    // 登出url
    //var loginOut = 'http://lego.iqiyi.com/logout';
    var baseInfo = {
        url: url,
        authUrl: 'http://test.admin.epg.qiyi.domain/api/permission',
        loginUrl: 'http://lego.iqiyi.com/login?url=http://lego.iqiyi.com%2F',
        url403: 'http://lego.iqiyi.com/common/403.jsp',
        loginOut: 'http://lego.iqiyi.com/logout',
        authInfo: null,
        tips: 0 //0 304页面提示 1头部标签提示
    }

    // 跳转到相应page
    var jump2TargetPage = function (url) {
        window.location.href = url;
    }

    // ajax 请求
    var getAuthData = function (options, callback) {

        options = options || {};

        baseInfo = $.extend(true, baseInfo, options);

        $.ajax({
            url: baseInfo.authUrl,
            dataType: 'json',
            method: 'get',
            cache: false,
            //async: false,
            data: {'uri': baseInfo.url.split('?')[0]},
            beforeSend: function (request) {
                var time = parseInt(new Date().getTime() / 1000);
                request.setRequestHeader('Sign', md5('/permission' + time + '7dpHGKMy4hAh3DmC'));
                request.setRequestHeader("Time", time);
                request.setRequestHeader("From", 'frontend');
            },

            success: function (data) {

                //console.log(data);

                if (baseInfo.tips) {

                    if (data.code == 'A00304')
                        callback.call(this, 1);   // 无权限
                    else
                        callback.call(this, null); // 有权限

                    return;
                }


                switch (data.code) {
                    case 'A00000': // 已登陆
                        baseInfo.authInfo = data.data;
                        showLogInfo(baseInfo);

                        var newDev = 0;
                        if (baseInfo.authInfo.devMode)
                            newDev = 1

                        var dev = getDev();

                        if (dev == NaN || dev != newDev) {
                            Com.cookie.set('dev', newDev);
                            window.location.reload();
                        }

                        break;

                    case 'A00303': // 未登陆
                      //  jump2TargetPage(baseInfo.loginUrl);
					  console.log("mockData");
var data = {"region":2,"uid":7000000209,"virtualReporter":0,"createTime":1423021320,"phone":"","zhan":null
,"updateTime":1423021320,"department":1,"gov_editor":null,"type":0,"effective":1,"reporter":0,"setting"
:null,"email":"karkarrotxu@qiyi.com","child_type":null,"userName":"许禕","thirdPartyId":null,"brand":3
,"devMode":true}
                        baseInfo.authInfo = data;
                        showLogInfo(baseInfo);

                        var newDev = 0;
                        if (baseInfo.authInfo.devMode)
                            newDev = 1

                        var dev = getDev();

                        if (dev == NaN || dev != newDev) {
                            Com.cookie.set('dev', newDev);
                            window.location.reload();
                        }
                        break;

                    case 'A00304': // 无权限
                        //jump2TargetPage(baseInfo.url403);
                        var warning = setWaring('sorry,您没有操作本页面的权限！');
                        $(document).ready(function () {
                            $('body').html('');
                            $('body').html(warning);
                        });

                        break;
                }


            },
            error: function (data) {

                var warning = setWaring('网络故障,<a href="javascript:void(0);" onclick = "window.location.reload();">请重刷！</a>');
                $(document).ready(function () {
                    $('body').html(warning);
                });
            }
        });
    };

    var getDev = function () {
        return parseInt(Com.cookie.get('dev'));
    };

    return{
        getAuthData: getAuthData,
        baseInfo: baseInfo,
        jump2TargetPage: jump2TargetPage,
        dev: getDev //开发环境

    };

})();
auth.getAuthData();
var SystemDialog = (function () {
    var doHandler = null;
    var _initAlert = false;
    var _initConfirm = false;

    function initAlertDlg() {
        if (!_initAlert) {
            if ($("#epg_alert_dlg").length == 0) {
                $('body').append('<div id="epg_alert_dlg"><div sign="detail"></div></div>');
            }
            $("#epg_alert_dlg").dialog({
                title: "epg系统提醒",
                autoOpen: false,
                width: 400,
                modal: true,
                dialogClass: 'alert',
                resizable: false,
                buttons: [
                    {
                        text: "确认",
                        click: function () {
                            $(this).dialog("close");
                        }
                    }
                ]
            })
            _initAlert = true;
        }
        console.log("_initAlert:" + _initAlert);
    }

    function initConfirmDlg() {
        if (!_initConfirm) {
            if ($("#epg_confirm_dlg").length == 0) {
                $('body').append('<div id="epg_confirm_dlg"><div sign="detail"></div></div>');
            }
            $("#epg_confirm_dlg").dialog({
                title: "epg系统确认",
                autoOpen: false,
                width: 400,
                modal: true,
                dialogClass: 'alert',
                resizable: false,
                buttons: [
                    {
                        text: "确认",
                        click: function () {
                            $(this).dialog("close");
                            doHandler(true);
                        }
                    },
                    {
                        text: "取消",
                        click: function () {
                            $(this).dialog("close");
                            doHandler(false);
                        }
                    }
                ]
            });
            _initConfirm = true;
        }
        console.log("_initConfirm:" + _initConfirm);
    }

    function queryAlert(msg) {
        initAlertDlg()
        $("#epg_alert_dlg").find("div[sign=detail]").html(msg);
        $("#epg_alert_dlg").dialog("open");
    }

    function queryConfirm(msg, callback) {
        initConfirmDlg();
        $("#epg_confirm_dlg").find("div[sign=detail]").html(msg);
        $("#epg_confirm_dlg").dialog("open");
        doHandler = function (flag) {
            if (typeof(callback) == "function") {
                callback(flag);
            }
        }
    }

    return {
        queryAlert: queryAlert,
        queryConfirm: queryConfirm
    }
})();
function showLogInfo(baseInfo) {
    var header = [

        '<div class="top">',
        '<div class="logo">',
        '<a href="http://lego.iqiyi.com/" alt="乐高首页"></a>',
        '</div>',
        '<div class="user">',
        '<input type="hidden" name="hidden_uid" value="'+baseInfo.authInfo.uid+'">',

            ' Hi, <a href="" class="name">' + baseInfo.authInfo.userName + '</a>&nbsp;',
        '[<a href="http://static.lego.iqiyi.com/help/lego-help-v1.docx" class="help">帮助</a>]&nbsp;',
            '[<a href="' + baseInfo.loginOut + '" class="help">退出</a>]',
        '</div>',

        '</div>',
        '<div class="nav">',
        '<ol>',
        '<li class="channel-index "><a href="http://lego.iqiyi.com/" class="">我的首页</a><i></i></li>',
        '<li class="channel-catalog selected"><a href="javascript:void(0)" class="selected">直播中心</a><i></i>',
        '<div class="panel"> <dl class="panel-video clearfix">',
        '<dt><strong>频道</strong></dt>',
        '<dd><a href="./channel.html">直播频道列表</a><br /><a href="./channel_edit.html">新建直播频道</a>',
        '</dd>',
        '</dl>',

        '<dl class="panel-live_program clearfix">',
        '<dt><strong>节目</strong></dt>',
        '<dd><a href="./program_library.html">节目库</a></dd>',
        '</dl>',

        '<dl class="panel-live_demand clearfix">',
        '<dt><strong>直转点</strong></dt>',
        '<dd><a href="http://lego.iqiyi.com/peditor/zhibo_list">直播转点播</a><a href="http://lego.iqiyi.com/sport/live/home">直播列表</a><a href="http://lego.iqiyi.com/sport/live/add">新建直播</a></dd>',
        '</dl>',


        '</div>',
        '</li>',
        '</ol>',
        '</div>'

    ].join('');


    $(document).ready(function () {
        var epg_version = "tag V2.2.2" + (auth.dev() ? "_test" : "_production")
        $("header").html(header);
        $('body').append('<div class="scroll-to-top scroll-to-top-hide" id="BackToTopButton"><a class="btn" href="#">返回顶部</a></div><footer>版本 ' + epg_version + ' | Copyright ©2015 <a href="http://www.iqiyi.com" target="_blank">iQIYI.com</a></footer>');
        $(window).bind('scroll', function () {
            Com.page.scrollToTop($("#BackToTopButton"));
        });
    });
}

var dev = auth.dev(); //当前环境设置
